#include "QGameObject.h"


QGameObject::QGameObject(void)
{
}

QGameObject::QGameObject(string fileName)
{
	ifstream map (fileName);

	_staticObject = new list<GameObject*>();
	_dynamicObject = new list<GameObject*>();

	if(map.is_open())
	{		
		float posX, posY; int width, height, value;
		int count;
		map>> count >> width >> height;
		int id; 
		for (int i = 0; i < count; i++)
		{
			//so thu tu dong - idObj -...
			map>> id >> value >> posX >> posY >> width >> height;		

			switch(value)
			{
			case 5:
				_staticObject->push_back(new LargeCandle(posX, posY));
				break;
			case 6:
				_staticObject->push_back(new Candle(posX, posY));
				break;
			case 7:
				_dynamicObject->push_back(new BlackLeopard(posX, posY));
				break;
			case 8:
				break;
			case 9: 
				_dynamicObject->push_back(new VampireBat(posX, posY));
				break;
			case 10:
				_dynamicObject->push_back(new Zombie(posX, posY));
				break;
			default:
				_staticObject->push_back(new Brick(posX, posY, width, height));
				break;
			}
		}

	}
}

void QGameObject::Draw(CCamera *camera)
{
	for (list<GameObject*>::iterator i = _staticObject->begin(); i != _staticObject->end(); i++)
	{
		GameObject* obj = (*i);
		//if(!(obj->posX + obj->width < camera->viewport.x || obj->posX > camera->viewport.x + G_ScreenWidth
		//	|| obj->posY - obj->height > camera->viewport.y || camera->viewport.y + G_ScreenHeight > obj->posY))
		{
			obj->Draw(camera);
		}
	}

	for (list<GameObject*>::iterator i = _dynamicObject->begin(); i != _dynamicObject->end(); i++)
	{
		GameObject* obj = (*i);
		if(/*!(obj->posX + obj->width < camera->viewport.x || obj->posX > camera->viewport.x + G_ScreenWidth
			|| obj->posY - obj->height > camera->viewport.y || camera->viewport.y + G_ScreenHeight > obj->posY)
			&& */obj->active)
		{
			obj->Draw(camera);
		}
	}
}
void QGameObject::Update(int deltaTime)
{
	for (list<GameObject*>::iterator i = _staticObject->begin(); i != _staticObject->end(); i++)
	{
		(*i)->Update(deltaTime);
	}

	for (list<GameObject*>::iterator i = _dynamicObject->begin(); i != _dynamicObject->end(); i++)
	{
		if((*i)->active)
		{
			(*i)->Update(deltaTime);
		}
	}
}

void QGameObject::Collision(int dt)
{
	for (list<GameObject*>::iterator i = _dynamicObject->begin(); i != _dynamicObject->end(); i++)
	{
		if((*i)->active)
		{
			(*i)->Collision((*_staticObject), dt);
		}
	}
}

QGameObject::~QGameObject(void)
{
}
